package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.DicType;
import com.jframe.cfg.model.Dictionary;
import com.jframe.cfg.model.Tablemapping;
import com.jframe.cfg.service.DictionaryService;
import com.jframe.cfg.service.DictypeService;
import com.jframe.cfg.service.TablemapService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.*;
import com.jframe.sys.utils.*;
import com.jframe.sys.webui.*;


public class DictionaryAction extends ExtendAction{
	
	private static String ACTION="dictionaryAction";
	private static String SERVICE="dictionaryService";
	private static Class<Dictionary> clazz=Dictionary.class;
	
	public void list(){
		List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
		List<Columns> commonColumns = new ArrayList<Columns>();
		frozenColumns.add(new Columns("ck","",40,""));
		commonColumns.add(new Columns("id", "编号", 100,""));
		commonColumns.add(new Columns("code", "代码", 150,""));
		commonColumns.add(new Columns("value","字典值",150,""));
		Grid grid = new Grid();
		grid.setUrl(this.getUrl(ACTION+".do?method=load"));
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/jframe/cfg/list/dicdataList.jsp");
	}
	
	public void load(){
		this.baseLoad(SERVICE,clazz);
	}
	
	public void save(){
		this.baseSave(SERVICE, clazz);
	}
	
	
	public void delete(){
		this.baseDelete(SERVICE);
	}
	
	public void getUpDictype(){
		DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");	
		List<DicType> ls = dictypeService.findAll();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(DicType dictype:ls){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", ""+dictype.getId());
			map.put("text", dictype.getCnname());
			list.add(map);
		}
		this.print(JsonUtil.list2Json(list));
	}
	
	public void getDateType(){
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		String json = TabmCache.getCombotreeJson("sm_sys_app");
		List<Tree> ls = JsonUtil.json2List(json, Tree.class);
		for(Tree tree:ls){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", "root"+tree.getId());
			map.put("text", tree.getText());
			List<Map<String,Object>> childNode = this.getdicType(tree.getId());
			map.put("children", childNode);
			if(childNode.size()>0){
				map.put("state", "closed");
			}
			maps.add(map);
		}
		this.print(JsonUtil.list2Json(maps));
	}
	
	private List<Map<String,Object>> getdicType(String typeId){
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");	
		List<DicType> treels = dictypeService.findListByUpIds(""+typeId);
		for(DicType dictype:treels){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", ""+dictype.getId());
			map.put("text", dictype.getCnname());	
			maps.add(map);
		}
		return maps;
	}
	
	public void getRightTrees(){	
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		String json = TabmCache.getCombotreeJson("sm_sys_app");
			List<Tree> ls = JsonUtil.json2List(json, Tree.class);
			String ids = "";			
			for(Tree tree:ls){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("id", "root"+tree.getId());
				map.put("name", tree.getText());
				map.put("pId", 0);
				ids +=tree.getId()+",";
				maps.add(map);
			}
			
			if(ls.size()>0){
				ids = ids.substring(0, ids.length()-1);
			}
			
			DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");	
			List<DicType> treels = dictypeService.findListByUpIds(ids);
			for(DicType dictype:treels){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("id", dictype.getId());
				map.put("name", dictype.getCnname());
				map.put("pId", "root"+dictype.getUpId());			
				maps.add(map);
			}
			this.print(JsonUtil.list2Json(maps));
	}
	
	public void getObjById(){
		DictionaryService dictionaryService = (DictionaryService)this.getBean("dictionaryService");	
		int id=StringUtil.parseInt(request.getParameter("id"));
		Dictionary dictionary = dictionaryService.load(id);
		this.print(JsonUtil.obj2Json(dictionary));
	}
	
	public void getCombotreeDicType(){
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		String json = TabmCache.getCombotreeJson("sm_sys_app");
			List<Tree> ls = JsonUtil.json2List(json, Tree.class);
			for(Tree tree:ls){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("id", "root"+tree.getId());
				map.put("text", tree.getText());
				map.put("children", this.getChildren(tree.getId()));
			}
			this.print(JsonUtil.list2Json(maps));
	}
	
	public List<Map<String,Object>> getChildren(String upId){
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");
		List<DicType> dicTypes = dictypeService.findListByUpIds(upId);
		TablemapService tablemapService = (TablemapService)this.getBean("tablemapService");		
		List<Tablemapping> Tablemappings = tablemapService.findListByUpIds(upId);
		for(DicType dicType:dicTypes){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", dicType.getEnname());
			map.put("text", dicType.getCnname());
			maps.add(map);
		}
		for(Tablemapping Tablemapping : Tablemappings){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", Tablemapping.getEnname());
			map.put("text", Tablemapping.getCnname());
			maps.add(map);
		}
		return maps;
	}

}
